package com.amigo.online.provider.merchant.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.amigo.online.common.param.result.UserInfo;
import com.amigo.online.common.param.result.merchant.CategoryInfo;
import com.amigo.online.common.param.result.merchant.CollectGoodsInfo;
import com.amigo.online.common.param.result.merchant.MerchantCollectInfo;
import com.amigo.online.common.param.result.merchant.MerchantCommentInfo;
import com.amigo.online.common.param.result.merchant.MerchantConcernListInfo;
import com.amigo.online.common.param.result.merchant.MerchantManagerInfo;
import com.amigo.online.provider.merchant.model.entity.Category;
import com.amigo.online.provider.merchant.model.entity.Goods;
import com.amigo.online.provider.merchant.model.entity.GoodsComment;
import com.amigo.online.provider.merchant.model.entity.GoodsCommentImg;
import com.amigo.online.provider.merchant.model.entity.GoodsImg;
import com.amigo.online.provider.merchant.model.entity.Merchant;
import com.amigo.online.provider.merchant.model.entity.MerchantManager;

public class ConvertUtil {

    public static MerchantConcernListInfo merTomerInfo(List<Merchant> list) {
        MerchantConcernListInfo info = new MerchantConcernListInfo();
        List<MerchantCollectInfo> collect = new ArrayList<>();
        if (list != null) {
            collect = list.stream()
                    .map(e -> new MerchantCollectInfo(e.getId(), e.getMerchantName(), e.getMerchantAvatar()))
                    .collect(Collectors.toList());
        }
        info.setList(collect);
        return info;
    }

    public static List<CollectGoodsInfo> goodsToMerCollectInfo(List<Goods> list) {
        List<CollectGoodsInfo> info = new ArrayList<>();
        for (Goods goods : list) {
            CollectGoodsInfo cgi = new CollectGoodsInfo();
            cgi.setMerchantId(goods.getMerchantId());
            cgi.setAnonymoused(1);
            if (goods.getStatus() != null && goods.getStatus() == 2) {
                cgi.setAnonymoused(0);
            }
            cgi.setCollectTime(goods.getGoodsCollect().getCreatedTime());
            cgi.setId(goods.getGoodsCollect().getId());
            cgi.setGoodsName(goods.getTitle());
            cgi.setPrice(goods.getPrice());
            cgi.setDiscountPrice(goods.getDiscountPrice());
            cgi.setGoodId(goods.getId());
            cgi.setGoodUrl(goods.getCoverImg());
            info.add(cgi);
        }
        return info;
    }

    public static List<MerchantCommentInfo> goodsCommentToCommentInfo(List<GoodsComment> list, UserInfo user, List<GoodsCommentImg> imgList) {
        List<MerchantCommentInfo> info = new ArrayList<>();
        if (list == null || list.size() == 0) {
            return info;
        }
        for (GoodsComment comment : list) {
            MerchantCommentInfo commentInfo = new MerchantCommentInfo();
            commentInfo.setAvatar(user.getAvatar());
            commentInfo.setNickName(user.getNickname());
            commentInfo.setUserId(user.getId());
            commentInfo.setCommentId(comment.getId());
            commentInfo.setComment(comment.getContent());
            commentInfo.setCommentTime(comment.getCreatedTime());
            commentInfo.setMerComment(comment.getBackContent());
            commentInfo.setMerCommentTime(comment.getBackTime());
            commentInfo.setGoodId(comment.getGoodsId());
            commentInfo.setGoodImg(comment.getGoods().getCoverImg());
            commentInfo.setGoodsName(comment.getGoods().getTitle());
            commentInfo.setMerchantId(comment.getGoods().getMerchantId());
            commentInfo.setMerchantName(comment.getGoods().getMerchantName());
            commentInfo.setPrice(comment.getGoods().getPrice());
            commentInfo.setDiscountPrice(comment.getGoods().getDiscountPrice());
            commentInfo.setStar(comment.getStar());
            commentInfo.setAnonymoused(1);
            if (comment.getGoods().getStatus() != null && comment.getGoods().getStatus() == 2) {
                commentInfo.setAnonymoused(0);
            }
            List<String> imgs = new ArrayList<>();
            if (imgList != null && imgList.size() > 0) {
                imgs = imgList.stream()
                        .filter(e -> e != null)
                        .filter(e -> e.getCommentId().equals(comment.getId()))
                        .map(e -> e.getImgUrl())
                        .collect(Collectors.toList());
            }
            commentInfo.setCommentUrlList(imgs);
            info.add(commentInfo);
        }
        return info;
    }

    public static List<MerchantCommentInfo> goodsCommentToCommentInfo2(List<GoodsComment> list, List<GoodsCommentImg> imgList) {
        List<MerchantCommentInfo> info = new ArrayList<>();
        if (list == null || list.size() == 0) {
            return info;
        }
        for (GoodsComment comment : list) {
            MerchantCommentInfo commentInfo = new MerchantCommentInfo();
            commentInfo.setAvatar(comment.getAvatar());
            commentInfo.setNickName(comment.getNickName());
            commentInfo.setUserId(comment.getUserId());
            commentInfo.setStar(comment.getStar());
            commentInfo.setCommentId(comment.getId());
            commentInfo.setComment(comment.getContent());
            commentInfo.setCommentTime(comment.getCreatedTime());
            commentInfo.setMerComment(comment.getBackContent());
            commentInfo.setMerCommentTime(comment.getBackTime());
            commentInfo.setGoodId(comment.getGoodsId());
            commentInfo.setGoodImg(comment.getGoods().getCoverImg());
            commentInfo.setGoodsName(comment.getGoods().getTitle());
            commentInfo.setMerchantId(comment.getGoods().getMerchantId());
            commentInfo.setMerchantName(comment.getGoods().getMerchantName());
            commentInfo.setPrice(comment.getGoods().getPrice());
            commentInfo.setDiscountPrice(comment.getGoods().getDiscountPrice());
            commentInfo.setStar(comment.getStar());
            commentInfo.setAnonymoused(1);
            if (comment.getGoods().getStatus() != null && comment.getGoods().getStatus() == 2) {
                commentInfo.setAnonymoused(0);
            }
            List<String> imgs = new ArrayList<>();
            if (imgList.size() > 0) {
                imgs = imgList.stream()
                        .filter(e -> e != null)
                        .filter(e -> e.getCommentId().equals(comment.getId()))
                        .map(e -> e.getImgUrl())
                        .collect(Collectors.toList());
            }
            commentInfo.setCommentUrlList(imgs);
            info.add(commentInfo);
        }
        return info;
    }

    public static List<MerchantManagerInfo> merchantManagerToManagerInfo(List<MerchantManager> list) {
        List<MerchantManagerInfo> info = new ArrayList<>();
        if (list == null || list.size() == 0) {
            return info;
        }
        info = list.stream()
                .filter(e -> e != null)
                .map(e -> new MerchantManagerInfo(e.getUserId(), e.getAvatar(), e.getNickname()))
                .collect(Collectors.toList());
        return info;
    }

    public static List<MerchantCommentInfo> goodsCommentToCommentInfo3(List<GoodsComment> list, List<GoodsCommentImg> imgList) {
        List<MerchantCommentInfo> info = new ArrayList<>();
        if (list == null || list.size() == 0) {
            return info;
        }
        for (GoodsComment comment : list) {
            MerchantCommentInfo commentInfo = new MerchantCommentInfo();
            commentInfo.setAvatar(comment.getAvatar());
            commentInfo.setNickName(comment.getNickName());
            commentInfo.setUserId(comment.getUserId());
            commentInfo.setCommentId(comment.getId());
            commentInfo.setComment(comment.getContent());
            commentInfo.setCommentTime(comment.getCreatedTime());
            commentInfo.setMerComment(comment.getBackContent());
            commentInfo.setMerCommentTime(comment.getBackTime());
            commentInfo.setGoodId(comment.getGoodsId());
            commentInfo.setGoodImg(comment.getGoods().getCoverImg());
            commentInfo.setGoodsName(comment.getGoods().getTitle());
            commentInfo.setMerchantId(comment.getGoods().getMerchantId());
            commentInfo.setMerchantName(comment.getGoods().getMerchantName());
            commentInfo.setPrice(comment.getGoods().getPrice());
            commentInfo.setDiscountPrice(comment.getGoods().getDiscountPrice());
            commentInfo.setStar(comment.getStar());
            commentInfo.setAnonymoused(comment.getGoods().getDeleted());
            List<String> imgs = new ArrayList<>();
            if (imgList.size() > 0) {
                imgs = imgList.stream()
                        .filter(e -> e != null)
                        .filter(e -> e.getCommentId().equals(comment.getId()))
                        .map(e -> e.getImgUrl())
                        .collect(Collectors.toList());
            }
            commentInfo.setCommentUrlList(imgs);
            info.add(commentInfo);
        }
        return info;
    }

    public static List<CategoryInfo> categoryToCategoryInfo(String language, List<Category> list) {
        List<CategoryInfo> result = new ArrayList<>();
        for (Category category : list) {
            CategoryInfo categoryInfo = new CategoryInfo();
            categoryInfo.setId(category.getId());
            categoryInfo.setImgUrl(category.getImgUrl());
            switch (language) {
                case "name_cn":
                    categoryInfo.setName(category.getNameCn());
                    break;
                case "name_pt":
                    categoryInfo.setName(category.getNamePt());
                    break;
                case "name_es":
                    categoryInfo.setName(category.getNameEs());
                    break;
                case "name_en":
                    categoryInfo.setName(category.getNameEn());
                    break;
                default:
                    categoryInfo.setName(category.getNamePt());
                    break;
            }
            result.add(categoryInfo);
        }
        return result;
    }


}